<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>DBtoDoc-详情</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <script src="/layui/layui.js"></script>
</head>
<body>
<div id="content" class="layui-row layui-anim layui-anim-upbit" style="width: 95vw;margin: 10px auto;">
  <h1 style="text-align: center;">[[${tableName+'  表'}]]</h1>
  <div class="layui-row" style="margin-top: 10px;">
    <table id="table" lay-filter="test"></table>
  </div>
</div>

<script>
  var $, table, tableData;
  layui.use(['table', 'jquery'], function () {
    $ = layui.jquery;
    table = layui.table;

    table.render({
      elem: '#table'
      , id: 'tableId'
      , limit: 999
      , url: '/main/getTableSchema' //数据接口
      , where: {'tableName': '[[${tableName}]]'}
      , parseData: function (res) { //res 即为原始返回的数据
        tableData = res.data.columnInfo
        res.data = res.data.columnInfo
        return res;
      }
      , cols: [
        [
          {field: 'columnName', title: '列名称', width: 200, sort: true, fixed: 'left'}
          , {field: 'columnType', title: '列类型', width: 155, sort: true}
          , {field: 'columnComment', title: '列注释', width: 300, sort: true}
          , {field: 'ordinalPosition', title: '排序', width: 80, sort: true}
          , {field: 'isNullable', title: '允许空', width: 90, sort: true}
          , {field: 'character', title: '编码', width: 100, sort: true}
          , {
          fixed: 'right', title: '操作', templet: function (res) {
            return '<button onclick="updateDescription(\'' + res.columnName
                + '\')" type="button" class="layui-btn layui-btn-normal layui-btn-sm">改注释</button>'
          }
        }
        ]
      ]
    });
  });

  function updateDescription(columnName) {
    var val = {}
    for (var i = 0; i < tableData.length; i++) {
      if (tableData[i].columnName === columnName) {
        val = tableData[i]
        break
      }
    }
    layer.prompt({
      title: '修改表注释',
      formType: 2
      , value: val.columnComment
    }, function (value, index) {
      $.ajax({
        data: {"tableName": '[[${tableName}]]', "columnName": columnName, "comment": value},
        url: '/main/updateColumnComment',
        type: 'post',
        sync: false,
        success: function (data) {
          if (data.code == 0) {
            table.reload('tableId');
            window.setTimeout(function () {
              layer.msg(data.msg)
            }, 600)
            layer.close(index);
          } else {
            alert(data.msg)
          }
        }
      })
    });
  }

</script>
</body>
</html>
